package com.gx.smart.building;

import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.SpareStockRecordApiGrpc;
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.SpareStockRecordOperateReq;
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.SpareStockRecordResp;
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.dto.SpareRecordDto;
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.dto.SpareStockRecordDto;
import com.orhanobut.logger.Logger;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/4/26
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class SpareStockRecordApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private static class SingleTonHolder {
        private static final SpareStockRecordApiService INSTANCE = new SpareStockRecordApiService();
    }

    public static SpareStockRecordApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    private SpareStockRecordApiService() {

    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private SpareStockRecordApiGrpc.SpareStockRecordApiBlockingStub getStub(ManagedChannel channel) {
        return SpareStockRecordApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 库存记录新增(同：备件申请（APP）)
     *"id":string"714155892172582912"
     *   "dictTag":string"stock_out_use"
     *   "dictName":string"领用出库"
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, SpareStockRecordResp> insertSpareStockRecord(
            String warehouseId,
            String warehouseCode,
            String warehouseName,
            String recordTime,
            String description,
            List<SpareRecordDto> spareRecords,
            CallBack<SpareStockRecordResp> callBack) {
        return new GrpcAsyncTask<String, Void, SpareStockRecordResp>(callBack) {
            @Override
            protected SpareStockRecordResp doRequestData(ManagedChannel channel) {
                SpareStockRecordOperateReq message = SpareStockRecordOperateReq
                        .newBuilder()
                        .setContent(SpareStockRecordDto
                                .newBuilder()
                                .setCategory(1)//记录类别 1出库 2入库
                                .setWarehouseId(warehouseId)
                                .setWarehouseName(warehouseName)
                                .setWarehouseCode(warehouseCode)
                                .setRecordTime(recordTime)
                                .setRecordTypeId("714155892172582912")
                                .setRecordTypeCode("stock_out_use")
                                .setRecordTypeName("领用出库")
                                .setDecription(description)
                                .setApplyReason(description)
                                .setApplyTime(recordTime)
                                .addAllContent(spareRecords)
                                .build())

                        .build();
                Logger.d(message);
                SpareStockRecordResp response = null;
                try {
                    response = getStub(channel).insertSpareStockRecord(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "insertSpareStockRecord");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }
}
